/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */

package loginbean;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;


public class PrintConfirmationService {

    	
    	Connection conn = null;
    	Statement st=null;
    	Statement st2=null;
    	Statement st3=null;
    	ResultSet rsPrice= null;
    	ResultSet rsProduct= null;
    	ResultSet rsTotal= null;
    	float result=0;
    	String returnValue="<h1>The purchase you have made is: </h1><br/>";
    	String[] priceArray = new String[4];
    	String[] productNameArray = new String[4];
    	int counter1 =0;
    	int counter2 =0;
    	
    	
        public String printConfirmation(int tenantid)
        {
    		try 
    		{
    			DBManager db = new DBManager();
    			conn = db.getDatabaseConnection();
    			
    			String strQuery3 = "Select value from DataTable where tenantid = " + tenantid + " And fieldid = 16;"; //using the Cart Object to get the CartTotal by tenantid
    			st3 = conn.createStatement();
    			rsTotal = st3.executeQuery(strQuery3);
    			
    			while (rsTotal.next())
    			{
    				result = Float.valueOf(rsTotal.getString("value").trim()).floatValue();
    						
    			}
    			
    			

    			String strQuery = "select value from datatable where tenantid = " + tenantid + " and GUID in (select (value - 1) as a from datatable where fieldID in (8,9,10,11) and value <> 0) order by GUID"; //using the Cart Object to get the CartTotal by tenantid and customerid
    			st = conn.createStatement();
    			rsPrice = st.executeQuery(strQuery);
    			
    			while (rsPrice.next())
    			{	
    				priceArray[counter1] = rsPrice.getString("value");
    				counter1++;
    				
    			}
    			
    			String strQuery2 = "select value from datatable where tenantid = " + tenantid + " and GUID in (select value as a from datatable where fieldID in (8,9,10,11) and value <> 0) order by GUID"; //using the Cart Object to get the CartTotal by tenantid and customerid
    			st2 = conn.createStatement();
    			rsProduct = st2.executeQuery(strQuery2);
    			
    			while (rsProduct.next())
    			{	
    				productNameArray[counter2] = rsProduct.getString("value");
    				counter2++;
    				
    			}
    			
    			
    			
    			
    			if (counter1 != counter2)
    			 returnValue += "An error existed in the Shopping Cart!";
    			else
    			{
    				for(int i=0;i<counter1;i++){
    					returnValue += productNameArray[i];
    					returnValue += " :	 ";
    					returnValue += priceArray[i];
    					returnValue += "<br/>";
    				}
    				
    				returnValue += "The total price after tax and coupon is: <br/>";
    				returnValue += result;
    				returnValue += "<br/>Thank you for your shopping";
    						
    				  				
    			}
    			
    			
    			
    			conn.close();
    	
    			
    		} 
    		catch (Exception e) 
    		{
    			e.printStackTrace();
    		}
    		
    		return returnValue;
    		
    		

        }
    }
